package com.yl.applicationsecurity.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yl.applicationsecurity.mapper.ScaVulnerabilityMapper;
import com.yl.applicationsecurity.mapper.ScriptStateMapper;
import com.yl.applicationsecurity.pojo.KunpengSqlInjection;
import com.yl.applicationsecurity.pojo.ScaVulnerability;
import com.yl.applicationsecurity.pojo.ScriptState;
import com.yl.applicationsecurity.service.ScaVulnerabilityService;
import com.yl.applicationsecurity.service.ScriptStateService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author 鹤仙问鹿仙
 * @program: Application-security
 * @description:
 * @date 2025-04-01 17:05:23
 */
@Service
public class ScaVulnerabilityServiceImpl  extends ServiceImpl<ScaVulnerabilityMapper, ScaVulnerability> implements ScaVulnerabilityService {
    @Override
    public PageInfo<ScaVulnerability> findListByScaVulnerability(String[] envArray,Integer[] statusArray, String[] nationSelect, ScaVulnerability scaVulnerability, Date startCreateDate, Date endCreateDate, Integer pageNum, Integer pageSize) {

        PageHelper.startPage(pageNum, pageSize );//每页显示8条

        LambdaQueryWrapper<ScaVulnerability> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        if ( StringUtils.isNotEmpty(scaVulnerability.getAppName())){
            lambdaQueryWrapper.like(ScaVulnerability::getAppName,scaVulnerability.getAppName());
        }

        if ( StringUtils.isNotEmpty(scaVulnerability.getVersion())){
            lambdaQueryWrapper.eq(ScaVulnerability::getVersion,scaVulnerability.getVersion());
        }

        if ( StringUtils.isNotEmpty(scaVulnerability.getJarName())){
            lambdaQueryWrapper.eq(ScaVulnerability::getJarName,scaVulnerability.getJarName());
        }

        if ( StringUtils.isNotEmpty(scaVulnerability.getProject())){
            lambdaQueryWrapper.eq(ScaVulnerability::getProject,scaVulnerability.getProject());
        }


        if (envArray!=null&&envArray.length!=0){
            lambdaQueryWrapper.in(ScaVulnerability::getEnv,envArray);
        }


        if (statusArray!=null&&statusArray.length!=0){
            lambdaQueryWrapper.in(ScaVulnerability::getStatus,statusArray);
        }

        if (null!=nationSelect&&nationSelect.length!=0){
            lambdaQueryWrapper.in(ScaVulnerability::getNation,nationSelect);
        }

        if(startCreateDate!=null){
            lambdaQueryWrapper.ge(ScaVulnerability::getCreateDate, startCreateDate);
        }


        if(endCreateDate!=null){
            lambdaQueryWrapper.le(ScaVulnerability::getCreateDate, endCreateDate);
        }

        lambdaQueryWrapper.orderByDesc(ScaVulnerability::getCreateDate);//根据首次发现时间排序
        List<ScaVulnerability> list = baseMapper.selectList(lambdaQueryWrapper);


        PageInfo<ScaVulnerability> pageInfo = new PageInfo<ScaVulnerability>(list);


        if (pageInfo.getPages() == 0) {
            pageInfo.setPages(1);
        }

        return pageInfo;
    }

    @Override
    public int updateStatus(String[] ids, Integer updateStatus) {
        LambdaUpdateWrapper<ScaVulnerability> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.set(ScaVulnerability::getStatus,updateStatus).in(ScaVulnerability::getId , ids);

        return baseMapper.update(null,lambdaUpdateWrapper);
    }

    @Override
    public int insert(ScaVulnerability scaVulnerability) {
        //需要首先判断当前插入的应用是否已存在,且已经加入了白名单
        LambdaQueryWrapper<ScaVulnerability> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ScaVulnerability::getAppName,scaVulnerability.getAppName());
        lambdaQueryWrapper.eq(ScaVulnerability::getJarName,scaVulnerability.getJarName());
        lambdaQueryWrapper.eq(ScaVulnerability::getEnv,scaVulnerability.getEnv());
        lambdaQueryWrapper.eq(ScaVulnerability::getNation,scaVulnerability.getNation());
        lambdaQueryWrapper.eq(ScaVulnerability::getVersion,scaVulnerability.getVersion());
        lambdaQueryWrapper.eq(ScaVulnerability::getProject,scaVulnerability.getProject());
        lambdaQueryWrapper.eq(ScaVulnerability::getStatus,3);

        Long l = baseMapper.selectCount(lambdaQueryWrapper);

        if(l>0){
            return 0;
        }
        //其次需要判断该数据在数据库当中是否存在,如果存在则不要重复插入
        lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ScaVulnerability::getId,scaVulnerability.getId());
        l = baseMapper.selectCount(lambdaQueryWrapper);

        if(l>0){
            return 0;
        }



        //scaVulnerability.setId(UUID.randomUUID().toString());   使用运维的ID做唯一ID
        scaVulnerability.setStatus(0);
        scaVulnerability.setCreateDate(new Date());
        return baseMapper.insert(scaVulnerability);
    }
}

